package org.compiere.report;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.compiere.model.MAssetAcct;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MBPartner;
import org.compiere.model.MOrg;
import org.compiere.model.X_A_Asset;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;


public class A_Asset_Form_NMA1 extends SvrProcess {

	/** Current context */
	private Properties m_ctx;
	/**     */
	private ProcessInfo pi;

	int p_A_Asset_ID;
	
	String getSQlresult = null;

	/** */
	String file;

	/**
	 * Prepare - e.g., get Parameters.
	 */
	@Override
	protected void prepare() {
		log.info("");
		m_ctx = Env.getCtx();
		pi = getProcessInfo();
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else if (name.equals("A_Asset_ID")) 
				p_A_Asset_ID  = para[i].getParameterAsInt();
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}
		// / here another code)
	}

	@Override
	protected String doIt() throws Exception {
		file = Util.localFilePath + "A_Asset_Form_INV1_tmp.xls";
		MAttachmentEntry entry = Util.getAttachment(pi, m_ctx, file);
		if (entry == null)
			throw new AdempiereException(Msg.translate(m_ctx,"NotFoundTemplate"));

		HSSFWorkbook wb = new HSSFWorkbook(entry.getInputStream());
		HSSFSheet sheet = wb.getSheetAt(0);
		Row R = null;
		Cell C = null;
		
		Timestamp currdate = new Timestamp(System.currentTimeMillis());
		X_A_Asset asset = new X_A_Asset (getCtx(), p_A_Asset_ID, get_TrxName());

		MOrg org = MOrg.get(m_ctx,asset.getAD_Org_ID());
		MBPartner bpartner = MBPartner.get(m_ctx, org.getLinkedC_BPartner_ID(get_TrxName()));
		R = sheet.getRow(9);
		C = R.getCell(37);
		C.setCellValue(bpartner.get_Value("FI_TIN").toString());
		R = sheet.getRow(10);
		C = R.getCell(0);
		C.setCellValue(org.getName());
		R = sheet.getRow(17);
		C = R.getCell(19);
		C.setCellValue(asset.getName());
		R = sheet.getRow(21);
		C = R.getCell(0);
		C.setCellValue(org.getName());
		R = sheet.getRow(25);
		C = R.getCell(0);
		C.setCellValue(new SimpleDateFormat("dd.MM.yyyy").format(
				asset.getAssetServiceDate()));
		
		BigDecimal bCost = asset.getAssetCost(currdate);
		if (bCost!=null) 
		{
			R = sheet.getRow(25);
			C = R.getCell(18);
			C.setCellValue(bCost.toString());
		} 
		
		String sql = "SELECT A_Asset_Acct_ID "
				+ "FROM A_Asset_Acct "
				+ "WHERE A_Asset_ID= " + p_A_Asset_ID
				+ "AND PostingType = 'A'"
				+ "AND IsActive = 'Y'";
		int iAcctID = DB.getSQLValue(get_TrxName(),sql);
		
		if (iAcctID>0) {
			MAssetAcct acct = new MAssetAcct (getCtx(), iAcctID, get_TrxName());
			R = sheet.getRow(25);
			C = R.getCell(38);
			C.setCellValue(acct.getA_Depreciation_Variable_Perc().setScale(2).toString());
		}

		BigDecimal bAccumDep = asset.getAccumDepreciation(currdate);
		if (bAccumDep!=null)
		{
			R = sheet.getRow(25);
			C = R.getCell(47);
			C.setCellValue(bAccumDep.toString());
			
		}
		
		// Write the output to a file
		FileOutputStream fileOut = new FileOutputStream(file);
		wb.write(fileOut);
		fileOut.close();
		try {
				//Forces Excel to use the specified file as a template for the default workbook. 
				Runtime.getRuntime().exec("cmd /c start excel " + file+" /t");
			} 
		catch (Exception e) 
			{
				e.printStackTrace();
			}
		return Msg.translate(m_ctx, "Success");
	}
}